const { baseUrl } = require('./http').dev
module.exports = {
  request: (url, method, data) => {
    let _url = `${baseUrl}/${url}`
    return new Promise((resolve, reject) => {
      wx.request({
        url: _url,
        data: data,
        method: method,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: (res) => {
          console.log('The data returned from the interface is------->', res);
          let { code } = res.data;
          switch (code) {
            case 200:
              resolve(res.data)
              break;
            default:
              wx.showToast({
                title: '请稍后再点哦~',
                icon: "error"
              })
          }
        },
        fail() {
          reject('接口有误，请检查')
        }
      })
    })
  },
  tokenRequest: (url, method, data) => {
    let _url = `${baseUrl}/${url}`
    console.log(_url)
    return new Promise((resolve, reject) => {
      wx.request({
        url: _url,
        data: data,
        method: method,
        header: {
          "Authorization": `Bearer ${wx.getStorageSync('token')}`
        },
        success: (res) => {
          console.log('The data returned from the interface is------->', res);
          let { code } = res.data;
          switch (code) {
            case 200:
              resolve(res.data);
              break;
             default:
                wx.showToast({
                  title: '请稍后再点哦~',
                  icon: "error"
                })
          }
        },
        fail() {
          reject('接口有误，请检查')
        }
      })
    })
  },
}